---
title: "Graphs for Confucian Long Peace"
author: "Daniel Druckman, Michael Gigante, Joshua Stone, Ming Wan (Order TBD)"
output: 
  pdf_document:
    latex_engine: lualatex
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
my.dir <- dirname(rstudioapi::getActiveDocumentContext()$path)
setwd(my.dir)
library(tidyverse)
library(knitr)
library(AER)
library(ggplot2)
library(dplyr)
library(ggpubr)
library(kableExtra)
library(xtable)
```

## Data cleaning 
Run a correlation analysis
```{r echo=FALSE, fig.align='center', message=FALSE, warning=FALSE, results='asis'}
# reading in the data
df <- read.csv("data/appendix_a_dataset.csv")

# changing the NAs to 0's
df[is.na(df)] <- 0

# ensuring that all variables are numeric, so we can add them up
df$conflict_ringai <- as.numeric(df$conflict_ringai)
df$conflict_internal <- as.numeric(df$conflict_internal)
df$conflict_conf <- as.numeric(df$conflict_conf)
df$peace_coop <- as.numeric(df$peace_coop)
df$peace_neu<- as.numeric(df$peace_neu)
df$peace_coerc <- as.numeric(df$peace_coerc)

#creating totals for all countries
df$conflict_total <- as.numeric(df$conflict_conf+df$conflict_internal+df$conflict_ringai)
df$peace_total <- as.numeric(df$peace_coerc+df$peace_coop+df$peace_neu)

#building different datasets that removes Japan, and creates the country-specific datasets  
df_no_japan <- df %>% filter(country!="japan")
df_vietnam <- df %>% filter(country=="vietnam")
df_china <- df %>% filter(country=="china")
df_japan <- df %>% filter(country=="japan")
df_korea <- df %>% filter(country=="korea")

# creating periods by 5 year segments
df <- df %>%
  mutate(five_years = cut(year, breaks = seq(1598, 1897, by = 5), labels = FALSE))

# creating year ranges for our periodization
year_ranges <- data.frame(
  period = c("1598-1677", "1678-1769", "1770-1860", "1861-1894", "1895-1897"),
  start_year = c(1598, 1678, 1770, 1861, 1895),
  end_year = c(1677, 1769, 1860, 1894, 1897))

# assigning labels by time period
assign_period_label <- function(year) {
  for (i in 1:nrow(year_ranges)) {
    if (year >= year_ranges$start_year[i] && year <= year_ranges$end_year[i]) {
      return(year_ranges$period[i])
    }
  }
  return("Other")
}

#mutating the database to apply time periods and assign labels
df <- df %>%
  mutate(period = sapply(year, assign_period_label)) %>%
  mutate(period = as.factor(period))

table(df$period)
unique(df$period)

df <- df %>% dplyr::select(event_id, country, peace_coerc, peace_neu, peace_coop, conflict_conf, peace_total, conflict_total, conflict_ringai, conflict_internal, event_info, gov_type, sources, period, year)
``` 

# Graphs
```{r echo=FALSE, fig.align='center', message=FALSE, warning=FALSE, results='asis'}
### conflict Graphs ###
# graph for conflict_conf
np1 <- ggplot(df, aes(x= period, y= conflict_conf, color=country)) +
  geom_point() +
  facet_wrap(facets =  vars(country)) +
  geom_smooth(method = loess) +
    ggtitle("Confucian conflict events")

# graph for conflict_ringai
np2 <- ggplot(df, aes(x= period, y= conflict_ringai, color=country)) +
  geom_point() +
  facet_wrap(facets =  vars(country)) +
  geom_smooth(method = loess) +
    ggtitle("Neighbor/Foreign conflict events")

# graph for conflict_internal
np3 <- ggplot(df, aes(x= period, y= conflict_internal, color=country)) +
  geom_point() +
  facet_wrap(facets =  vars(country)) +
  geom_smooth(method = loess) +
    ggtitle("Internal conflict events")

# graph for conflict_total peace
np4 <- ggplot(df, aes(x= period, y= conflict_total, color=country)) +
  geom_point() +
  geom_smooth(method = loess) +
  facet_wrap(facets =  vars(country)) +
    ggtitle("Total conflict events")

### peace Graphs ###
# graph for peace_coerc
pp1 <- ggplot(df, aes(x= period, y= peace_coerc, color=country)) +
  geom_point() +
  facet_wrap(facets =  vars(country)) +
  geom_smooth(method = loess) +
    ggtitle("Coercive peace events")

# graph for peace_neu
pp2 <- ggplot(df, aes(x= period, y= peace_neu, color=country)) +
  geom_point() +
  facet_wrap(facets =  vars(country)) +
  geom_smooth(method = loess) +
    ggtitle("Neutral peace events")

# graph for peace_coop
pp3 <- ggplot(df, aes(x= period, y= peace_coop, color=country)) +
  geom_point() +
  facet_wrap(facets =  vars(country)) +
  geom_smooth(method = loess) +
    ggtitle("Coop peace events")

# graph for peace_total
pp4 <- ggplot(df, aes(x= period, y= peace_total, color=country)) +
  geom_point() +
  geom_smooth(method = loess) +
  facet_wrap(facets =  vars(country)) +
    ggtitle("Total peace events")

### Comparative Graphs ###
# comparative graph for peace/Negative Split Up by Country
pos_neg_per_country <- ggplot(df, aes(x=year)) + 
  geom_point(aes(y = peace_total, colour = "peace events")) +
  geom_point(aes(y = conflict_total, colour = "conflict events")) +
  geom_smooth(aes(y = peace_total, colour = "peace events")) + 
  geom_smooth(aes(y = conflict_total, colour = "conflict events")) +
  facet_wrap(facets =  vars(country)) +
  coord_cartesian(ylim = c(0, 2))

# comparative graph for peace/Negative for all countries
pos_conflict_total <- ggplot(df, aes(x=year)) + 
  geom_point(aes(y = peace_total, colour = "peace events")) +
  geom_point(aes(y = conflict_total, colour = "conflict events")) +
  geom_smooth(aes(y = peace_total, colour = "peace events")) + 
  geom_smooth(aes(y = conflict_total, colour = "conflict events")) +
  coord_cartesian(ylim = c(0, 2))

## Graph for determining periodizations using a loess method ##
trend_graph <- ggplot(df, aes(x=year)) +
  geom_smooth(aes(y = peace_total, colour = "peace events"), se = FALSE) + 
  geom_smooth(aes(y = conflict_total, colour = "conflict events"), se = FALSE) +
  annotate("rect", xmin = 1598, xmax = 1677, ymin = 0, ymax = 1, fill = "grey55", alpha = 0.3) +
  annotate("rect", xmin = 1678, xmax = 1769, ymin = 0, ymax = 1, fill = "grey30", alpha = 0.3) +
  annotate("rect", xmin = 1770, xmax = 1860, ymin = 0, ymax = 1, fill = "grey20", alpha = 0.3) +
  annotate("rect", xmin = 1861, xmax = 1894, ymin = 0, ymax = 1, fill = "grey10", alpha = 0.3) + 
  #annotate("text", x = 1620, y = .95, label = "Period 1", hjust = 0, size = 3, family = "Times New Roman") +
  #annotate("text", x = 1720, y = .95, label = "Period 2", hjust = 0, size = 3, family = "Times New Roman") +
  #annotate("text", x = 1810, y = .95, label = "Period 3", hjust = 0, size = 3, family = "Times New Roman") +
  #annotate("text", x = 1865, y = .95, label = "Period 4", hjust = 0, size = 3, family = "Times New Roman") +
  scale_x_continuous(breaks=seq(1598, 1894, 5)) +
  scale_y_continuous(labels=scales::comma) +
  scale_colour_manual(values = c("black", "darkgrey")) +  # Set colors to greyscale
  theme_classic() +
  labs(y = "Event Propensity", x = "Year", colour = "Event Type") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, family = "Times New Roman", size = 8),  # Slant x-axis labels and change font
        legend.position = "bottom",
        text = element_text(family = "Times New Roman"))  # Change overall font to Times New Roman

trend_graph
```

```{r}
## Graph for China, alone
graph_china <- ggplot(df_china, aes(x=year)) +
  geom_point(aes(y = peace_total, colour = "peace events")) +
  geom_point(aes(y = conflict_total, colour = "conflict events")) +
  geom_smooth(aes(y = peace_total, color = "Peace events"), method = "loess") + 
  geom_smooth(aes(y = conflict_total, color = "Conflict events"), method = "loess") +
  annotate("rect", xmin = (year=1598), xmax = (year=1678), ymin = 0, ymax = 1, fill = "grey55", alpha = 0.3) +
  annotate("rect", xmin = (year=1678), xmax = (year=1769), ymin = 0, ymax = 1, fill = "grey30", alpha = 0.3) +
  annotate("rect", xmin = (year=1769), xmax = (year=1861), ymin = 0, ymax = 1, fill = "grey20", alpha = 0.3) +
  annotate("rect", xmin = (year=1861), xmax = (year=1897), ymin = 0, ymax = 1, fill = "grey10", alpha = 0.3) + 
  scale_x_continuous(breaks=seq(1598,1897,5))+
  scale_y_continuous(labels=scales::comma) +
  scale_color_manual(values = c("Peace events" = "white", "Conflict events" = "black")) +
  labs(x = "Year", y = "Event instances per year", color = "Event Type") +
  theme_classic() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  coord_cartesian(ylim = c(0, NA))
graph_china

## Graph for Korea, alone
graph_korea <- ggplot(df_korea, aes(x=year)) +
  geom_point(aes(y = peace_total, colour = "peace events")) +
  geom_point(aes(y = conflict_total, colour = "conflict events")) +
  geom_smooth(aes(y = peace_total, color = "Peace events"), method = "loess") + 
  geom_smooth(aes(y = conflict_total, color = "Conflict events"), method = "loess") +
  annotate("rect", xmin = (year=1598), xmax = (year=1678), ymin = 0, ymax = 1, fill = "grey55", alpha = 0.3) +
  annotate("rect", xmin = (year=1678), xmax = (year=1769), ymin = 0, ymax = 1, fill = "grey30", alpha = 0.3) +
  annotate("rect", xmin = (year=1769), xmax = (year=1861), ymin = 0, ymax = 1, fill = "grey20", alpha = 0.3) +
  annotate("rect", xmin = (year=1861), xmax = (year=1897), ymin = 0, ymax = 1, fill = "grey10", alpha = 0.3) + 
  scale_x_continuous(breaks=seq(1598,1897,5))+
  scale_y_continuous(labels=scales::comma) +
  scale_color_manual(values = c("Peace events" = "white", "Conflict events" = "black")) +
  labs(x = "Year", y = "Event instances per year", color = "Event Type") +
  theme_classic() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  coord_cartesian(ylim = c(0, NA))
graph_korea

## Graph for Japan, alone
graph_japan <- ggplot(df_japan, aes(x=year)) +
  geom_point(aes(y = peace_total, colour = "peace events")) +
  geom_point(aes(y = conflict_total, colour = "conflict events")) +
  geom_smooth(aes(y = peace_total, color = "Peace events"), method = "loess") + 
  geom_smooth(aes(y = conflict_total, color = "Conflict events"), method = "loess") +
  annotate("rect", xmin = (year=1598), xmax = (year=1678), ymin = 0, ymax = 1, fill = "grey55", alpha = 0.3) +
  annotate("rect", xmin = (year=1678), xmax = (year=1769), ymin = 0, ymax = 1, fill = "grey30", alpha = 0.3) +
  annotate("rect", xmin = (year=1769), xmax = (year=1861), ymin = 0, ymax = 1, fill = "grey20", alpha = 0.3) +
  annotate("rect", xmin = (year=1861), xmax = (year=1897), ymin = 0, ymax = 1, fill = "grey10", alpha = 0.3) +
  scale_x_continuous(breaks=seq(1598,1897,5))+
  scale_y_continuous(labels=scales::comma) +
  scale_color_manual(values = c("Peace events" = "white", "Conflict events" = "black")) +
  labs(x = "Year", y = "Event instances per year", color = "Event Type") +
  theme_classic() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  coord_cartesian(ylim = c(0, NA))
graph_japan

## Graph for Vietnam, alone
graph_vietnam <- ggplot(df_vietnam, aes(x=year)) +
  geom_point(aes(y = peace_total, colour = "peace events")) +
  geom_point(aes(y = conflict_total, colour = "conflict events")) +
  geom_smooth(aes(y = peace_total, color = "Peace events"), method = "loess") + 
  geom_smooth(aes(y = conflict_total, color = "Conflict events"), method = "loess") +
  annotate("rect", xmin = (year=1598), xmax = (year=1678), ymin = 0, ymax = 1, fill = "grey55", alpha = 0.3) +
  annotate("rect", xmin = (year=1678), xmax = (year=1769), ymin = 0, ymax = 1, fill = "grey30", alpha = 0.3) +
  annotate("rect", xmin = (year=1769), xmax = (year=1861), ymin = 0, ymax = 1, fill = "grey20", alpha = 0.3) +
  annotate("rect", xmin = (year=1861), xmax = (year=1897), ymin = 0, ymax = 1, fill = "grey10", alpha = 0.3) +
  scale_x_continuous(breaks=seq(1598,1897,5))+
  scale_y_continuous(labels=scales::comma, expand = c(0, 0)) +
  scale_color_manual(values = c("Peace events" = "white", "Conflict events" = "black")) +
  labs(x = "Year", y = "Event instances per year", color = "Event Type") +
  theme_classic() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  coord_cartesian(ylim = c(0, 1.05))
graph_vietnam
```


# Frequency Tables based on our Periods
```{r include=FALSE}
#frequencies for peace
#first period 1598 to 1677
df %>% filter(year<=1677) %>% count(peace_coerc)
df %>% filter(year<=1677) %>% count(peace_coop)
df %>% filter(year<=1677) %>% count(peace_neu)
df %>% filter(year<=1677, country=="japan") %>% count(peace_coerc)
df %>% filter(year<=1677, country=="japan") %>% count(peace_coop)
df %>% filter(year<=1677, country=="japan") %>% count(peace_neu)

#second period, 1678-1774
df %>% filter(year<=1774 & year>=1678) %>% count(peace_coerc)
df %>% filter(year<=1774 & year>=1678) %>% count(peace_coop)
df %>% filter(year<=1774 & year>=1678) %>% count(peace_neu)
df %>% filter(year<=1774 & year>=1678, country=="japan") %>% count(peace_coerc)
df %>% filter(year<=1774 & year>=1678, country=="japan") %>% count(peace_coop)
df %>% filter(year<=1774 & year>=1678, country=="japan") %>% count(peace_neu)

#third period, 1775-1842
df %>% filter(year<=1842 & year>=1775) %>% count(peace_coerc)
df %>% filter(year<=1842 & year>=1775) %>% count(peace_coop)
df %>% filter(year<=1842 & year>=1775) %>% count(peace_neu)
df %>% filter(year<=1842 & year>=1775, country=="japan") %>% count(peace_coerc)
df %>% filter(year<=1842 & year>=1775, country=="japan") %>% count(peace_coop)
df %>% filter(year<=1842 & year>=1775, country=="japan") %>% count(peace_neu)

#fourth period, 1843-1894
df %>% filter(year<=1894 & year>=1843) %>% count(peace_coerc)
df %>% filter(year<=1894 & year>=1843) %>% count(peace_coop)
df %>% filter(year<=1894 & year>=1843) %>% count(peace_neu)
df %>% filter(year<=1894 & year>=1843, country=="japan") %>% count(peace_coerc)
df %>% filter(year<=1894 & year>=1843, country=="japan") %>% count(peace_coop)
df %>% filter(year<=1894 & year>=1843, country=="japan") %>% count(peace_neu)

#frequencies for conflict
#first period 1598 to 1677
df %>% filter(year<=1677) %>% count(conflict_conf)
df %>% filter(year<=1677) %>% count(conflict_ringai)
df %>% filter(year<=1677) %>% count(conflict_internal)
df %>% filter(year<=1677, country=="japan") %>% count(conflict_conf)
df %>% filter(year<=1677, country=="japan") %>% count(conflict_ringai)
df %>% filter(year<=1677, country=="japan") %>% count(conflict_internal)

#second period, 1678-1774
df %>% filter(year<=1774 & year>=1678) %>% count(conflict_conf)
df %>% filter(year<=1774 & year>=1678) %>% count(conflict_ringai)
df %>% filter(year<=1774 & year>=1678) %>% count(conflict_internal)
df %>% filter(year<=1774 & year>=1678, country=="japan") %>% count(conflict_conf)
df %>% filter(year<=1774 & year>=1678, country=="japan") %>% count(conflict_ringai)
df %>% filter(year<=1774 & year>=1678, country=="japan") %>% count(conflict_internal)

#third period, 1775-1842
df %>% filter(year<=1842 & year>=1775) %>% count(conflict_conf)
df %>% filter(year<=1842 & year>=1775) %>% count(conflict_ringai)
df %>% filter(year<=1842 & year>=1775) %>% count(conflict_internal)
df %>% filter(year<=1842 & year>=1775, country=="japan") %>% count(conflict_conf)
df %>% filter(year<=1842 & year>=1775, country=="japan") %>% count(conflict_ringai)
df %>% filter(year<=1842 & year>=1775, country=="japan") %>% count(conflict_internal)

#fourth period, 1843-1894
df %>% filter(year<1895 & year>1842) %>% count(conflict_conf)
df %>% filter(year<1895 & year>1842) %>% count(conflict_ringai)
df %>% filter(year<1895 & year>1842) %>% count(conflict_internal)
df %>% filter(year<1895 & year>1842, country=="japan") %>% count(conflict_conf)
df %>% filter(year<1895 & year>1842, country=="japan") %>% count(conflict_ringai)
df %>% filter(year<1895 & year>1842, country=="japan") %>% count(conflict_internal)

#creating the frequency tables based on period
variable_counts_neg <- df %>%
  group_by(period) %>%
  summarize(
    conflict_conf_count = sum(conflict_conf == 1, na.rm = TRUE),
    conflict_ringai_count = sum(conflict_ringai == 1, na.rm = TRUE),
    conflict_internal_count = sum(conflict_internal == 1, na.rm = TRUE),
    conflict_total_count = sum(conflict_total == 1, na.rm = TRUE)
  ) %>%
  ungroup()

variable_counts_neg

#for peace
variable_counts_pos <- df %>%
  group_by(period) %>%
  summarize(
    peace_coerc_count = sum(peace_coerc == 1, na.rm = TRUE),
    peace_neu_count = sum(peace_neu== 1, na.rm = TRUE),
    peace_coop_count = sum(peace_coop == 1, na.rm = TRUE),
    peace_total_count = sum(peace_total == 1, na.rm = TRUE)
  ) %>%
  ungroup()

variable_counts_pos

```

```{r echo=FALSE, fig.align='center', message=FALSE, warning=FALSE}
#showing the tables for peace counts by period
table_pos <- variable_counts_pos %>%
  kable("latex") %>%
  kable_styling(bootstrap_options = "striped", full_width = FALSE)
table_pos

#showing the tables for conflict counts by period
table_neg <- variable_counts_neg %>%
  kable("latex") %>%
  kable_styling(bootstrap_options = "striped", full_width = FALSE)
table_neg
```

